package com.hjc.service.impl;

import com.hjc.entity.ProductType;
import com.hjc.entity.Result;
import com.hjc.mapper.ProduceTypeMapper;
import com.hjc.service.ProduceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = "com.hjc.service.impl.ProduceTypeServiceImpl")
@Service
public class ProduceTypeServiceImpl implements ProduceTypeService {
    @Autowired
    private ProduceTypeMapper produceTypeMapper;

    @Cacheable(key = "'all:category'")
    @Override
    public List<ProductType> findAllProductType() {
        List<ProductType> allCategoryList = produceTypeMapper.selectAllProductType();
        List<ProductType> categoryTree = categoryListToTree(allCategoryList,0);
        return categoryTree;
    }

    public List<ProductType> categoryListToTree(List<ProductType> allCategoryList, Integer parentId) {
        List<ProductType> categoryTreeRow = new ArrayList<>();
        for (ProductType productType : allCategoryList) {
            if (productType.getParentId().equals(parentId)) {
                categoryTreeRow.add(productType);
            }
        }

        for (ProductType productType : categoryTreeRow) {
            List<ProductType> childCategoryList = categoryListToTree(allCategoryList,productType.getTypeId());
            productType.setChildProductCategory(childCategoryList);
        }

        return categoryTreeRow;
    }

    @Override
    public Result selectProductTypeByCode(String typeCode) {
        ProductType oldProductType = produceTypeMapper.selectProductTypeByCode(typeCode);
        return Result.ok(oldProductType == null);
    }

    @CacheEvict(key = "'all:category'")
    @Override
    public Result addProductType(ProductType productType) {
        ProductType oldProductType = produceTypeMapper.selectProductTypeByName(productType.getTypeName());
        if(oldProductType != null) {
            return Result.err(Result.CODE_ERR_BUSINESS, "分类名称已经存在！");
        }
//        名称不重复
        int flag = produceTypeMapper.insertProductType(productType);
        if (flag > 0) {
            return Result.ok("新增成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "新增失败！");
    }

    @CacheEvict(key = "'all:category'")
    @Override
    public Result removeProductTypeById(Integer typeId) {
        int flag = produceTypeMapper.deleteProductTypeById(typeId);
        if (flag > 0) {
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！");
    }

    @CacheEvict(key = "'all:category'")
    @Override
    public Result setProductType(ProductType productType) {
        int flag = produceTypeMapper.updateProductType(productType);
        if (flag > 0) {
            return Result.ok("更新成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"更新失败！");
    }

}
